<?php
	namespace Util;

	class ValidateConf{
		/**
	     * 数据库对象实例
	     */    
		private $db;
		private $tabName;
		/**
	     * 视图实例对象
	     */    
		protected $view = null;
		/**
		 * [__construct 初始化数据源,生成页面]
		 * @Author   :seven
		 * @DateTime :2016-06-13T13:49:17+0800
		 * @Detail   :
		 */
		
		private $Str = "$('#FrmAuto').bootstrapValidator({
	message:'This value is not  valid',
	feedbackIcons:{   //图标
		valid:'glyphiconglyphicon-ok',
		invalid:'glyphiconglyphicon-remove',
		validating:'glyphiconglyphicon-refresh'
	},
	fields:{
";
		public function __construct($tabName){
			$this->tabName = $tabName;
			$this->view = \Think\Think::instance('Think\View');
			if(!isset($tabName) || empty($tabName)){
				throw_exception("Undefine the Table Name!");
			}else{
				if(is_string($tabName)){
					$this->db = M($tabName);
				}else{
					$this->db = M($tabName[0],$tabName[1]);
				}
			}
		}

		private function reverse(){
			$fields = S("validate_".$this->tabName);
	        /*仅调用一次,生成XML文件*/
	        if(!$fields){
	        	$this->db->table(C("DB_PREFIX").'goods');
		        $fields = $this->db->Reverse();
		        $fields = $fields['_type'];

		        /*
		    	1:字符串
		    	2:日期
		    	3:数字
		    	 */
		    	foreach ($fields as $k => $v) {
		    		if(strpos($v, 'char')){
		    			$fields[$k] = 1;
		    		}elseif(strpos($v, 'date')){
		    			$fields[$k] = 2;
		    		}else{
		    			$fields[$k] = 3;
		    		}
		    	}
	        }
	        return $fields;
	    }

	    /**
	     * [create 生成可视化操作页面]
	     * @Author   :seven
	     * @DateTime :2016-06-13T13:53:19+0800
	     * @Detail   :
	     */
	    public function create(){
	    	$fields = $this->reverse();

	    	$this->view->assign('fields',$fields);
	    	$this->view->display(THINK_PATH.'Tpl/validate.html');
	    }

	    public function Handle($data){
	    	$res = array();
	    	$index = 0;
	    	$fields = $this->reverse();
	    	foreach ($fields as $k => $v) {
	    		foreach ($data as $key => $value) {
	    			$t_k = substr($key, 0,strlen($k));
	    			if($t_k == $k){
	    				$res[$index][$key] = $value;
	    			}
	    		}
	    		$index++;
	    	}
	    
	    	foreach($res as $k=>$v){
	    		$t_k = explode("_", key($v));
	    		if($fields[$t_k[0]] == 1){
	    			$this->validateStr($v,$t_k[0]);
	    		}elseif($fields[$t_k[0]] == 2){
	    			$this->validateDat($v,$t_k[0]);
	    		}elseif($fields[$t_k[0]] == 3){
	    			$this->validateInt($v,$t_k[0]);
	    		}
	    	}
	    	$this->Str .= "
	}
});";
	    	Util("Validate\FileUtil");
        	\Util\FileUtil::logFile($this->Str,$this->tabName."_Auto_Validate");
	    }

	    private function validateStr($value,$key){
	    	$usekey = strtoupper($key);
	    	$this->Str .= "{$usekey}:{
	message:'The {$usekey} is notvalid',
	validators:{
		";

	    	foreach ($value as $k => $v) {
	    		if($k == $key."_Empty"){
	    			$this->Str .= $v == 'true'?"notEmpty:{
			message:'{$usekey}不能为空'
		},
		":'';

	    		}elseif($k == $key."_type"){
	    			if($v == "phone"){
	    				$this->Str .= "regexp:{
			regexp:/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/,
			message:'手机号码格式不对'
		},
		";
	    			}elseif($v == 'email'){
	    				$this->Str .= "emailAddress:{
			message:'{$usekey}不是邮件格式'
		},
		";
	    			}
	    		}elseif($k == $key.'_length'){
	    			if($v != ''){
	    				$t_min = $value[$key.'_length_min'];
	    				$t_max = $value[$key.'_length_max'];;
	    				$this->Str .= "stringLength:{
			min:{$t_min},
			max:{$t_max},
			message:'{$usekey}长度为{$t_min}-{$t_max}'
		},
		";
	    			}
	    		}elseif($k == $key.'_same'){
	    			$t_n = $value[$key.'_name'];
	    			if($v == "different"){
	    				$this->Str .= "different:{
			field:'{$t_n}',
			message:'不能和{$t_n}相同'
		},
		";
	    			}elseif($v == 'same'){
	    				$this->Str .= "identical:{
			field:'{$t_n}',
			message:'和{$t_n}不一样'
		},";
	    			}
	    		}
	    	}
	    	$this->Str.="
	}
},
";

	    }

	    private function validateDat(){
	    	
	    }

	    private function validateInt($value,$key){
	    	$usekey = strtoupper($key);
	    	$this->Str .= "{$usekey}:{
	message:'The {$usekey} is notvalid',
	validators:{
		";
	    	foreach ($value as $k => $v) {
	    		if($k == $key."_Empty"){
	    			$this->Str .= $v != ''?"notEmpty:{
		    message:'{$usekey}不能为空'
		},
		":'';
	    		}elseif($k == $key."_lr1"){
	    			if($v == ''){

	    			}elseif($v == 'lg'){
	    				$t_v = $value[$key.'_lr1_input'];
	    				$this->Str .= "greaterThan:{
			value:{$t_v},
			inclusive:false,
			message:'必须大于{$t_v}'
		},
		";
	    			}elseif($v == 'rg'){
	    				$t_v = $value[$key.'_lr1_input'];
	    				$this->Str .= "lessThan:{
			value:{$t_v},
			inclusive:false,
			message:'必须小于{$t_v}'
		},
		";
	    			}elseif($v == 'le'){
	    				$t_v = $value[$key.'_lr1_input'];
	    				$this->Str .= "greaterThan:{
			value:{$t_v},
			inclusive:true,
			message:'必须大于等于{$t_v}'
		},
		";
	    			}elseif($v == 're'){
	    				$t_v = $value[$key.'_lr1_input'];
	    				$this->Str .= "lessThan:{
			value:{$t_v},
			inclusive:true,
			message:'必须小于等于{$t_v}'
		},
		";
	    			}
	    		}elseif($k == $key."_lr2"){
	    			if($v == ''){

	    			}elseif($v == 'lg'){
	    				$t_v = $value[$key.'_lr2_input'];
	    				$this->Str .= "greaterThan:{
			value:{$t_v},
			inclusive:false,
			message:'必须大于{$t_v}'
		},
		";
	    			}elseif($v == 'rg'){
	    				$t_v = $value[$key.'_lr2_input'];
	    				$this->Str .= "lessThan:{
			value:{$t_v},
			inclusive:false,
			message:'必须小于{$t_v}'
		},
		";
	    			}elseif($v == 'le'){
	    				$t_v = $value[$key.'_lr2_input'];
	    				$this->Str .= "greaterThan:{
			value:{$t_v},
			inclusive:true,
			message:'必须大于等于{$t_v}'
		},
		";
	    			}elseif($v == 're'){
	    				$t_v = $value[$key.'_lr2_input'];
	    				$this->Str .= "lessThan:{
			value:{$t_v},
			inclusive:true,
			message:'必须小于等于{$t_v}'
		},
		";
	    			}
	    		}
	    	}
	    	$this->Str.="
	}
},
";
	    }
	}
	?>